package com.vectz.autogen.action;

import java.util.ArrayList;

import com.vectz.autogen.model.Entity;
import com.vectz.autogen.model.EntityOverview;
import com.vectz.autogen.util.Constant;
import com.vectz.autogen.util.Log;
import com.vectz.autogen.util.Util;

/**
 * @author ai-t.vn
 * 
 */
public class IActionCodegenAction {
	ArrayList<Entity> listField=null;
	EntityOverview table = null;
	String outputFolder = "";
	public IActionCodegenAction(ArrayList<Entity> listField, EntityOverview table, String outputFolder) {
		this.listField = listField;
		this.table = table;
		this.outputFolder = outputFolder;
		genernateIActionFile();
	}

	/*
	 * Generate java interface action file
	 */
	public void genernateIActionFile() {
		try {
			String tmplFormContent = Util.readTextFileContent(Util
					.getTmplPath("action", "IAction-File.txt"));

			
			tmplFormContent = tmplFormContent.replaceAll("xxxxPackageName",table.getPackageName());
			tmplFormContent = tmplFormContent.replaceAll("xxxxEntity", table.getName());
			tmplFormContent = tmplFormContent.replaceAll("xxxxLEntity", Util.lowerCaseFirstChar(table.getName()));
			tmplFormContent = tmplFormContent.replaceAll("xxxxPkType", Util.getPKType(listField));
			
			String fileLocation = new StringBuffer().append(outputFolder)
			.append(System.getProperty("file.separator"))
			.append("src")
			.append(System.getProperty("file.separator"))
			.append("action")
			.append(System.getProperty("file.separator"))
			.append("local")
			.append(System.getProperty("file.separator"))
			.append("I")
			.append(table.getName())
			.append("Action.java").toString();
			
			Util.writeJsfFile(fileLocation,tmplFormContent);
			Log.log("Finish generate java file to:"+fileLocation);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
